package demo.classuse;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/6-08-06-10:56
 * @Description：demo.classuse
 */
public class ObjectPool {
    public static Map<String, Object> map =  new HashMap<>();
    //初始化池子
    public void initPool(Map<String, String> map) throws Exception {
        for (String s : map.keySet()) {
            Class c = Class.forName(map.get(s));
            this.map.put(s,c.newInstance());
        }
    }

    public Object getObject(String key){
        if(map.containsKey(key)){
            return map.get(key);
        }else{
            throw new RuntimeException("对象池中不包含该对象，");
        }
    }
    public static void main(String[] args) throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("student","demo.classuse.Student");
        map.put("teacher","demo.classuse.Teacher");

        ObjectPool pool = new ObjectPool();
        pool.initPool(map);

        System.out.println(pool.getObject("student"));
        System.out.println(pool.getObject("teacher"));
        System.out.println(pool.getObject("hjy"));
    }
}
